package com.jilijili.controller;


import com.jilijili.grace.result.GraceJSONResult;
import com.jilijili.grace.result.ResponseStatusEnum;
import com.jilijili.pojo.Users;
import com.jilijili.service.FansService;
import com.jilijili.service.UsersService;
import com.jilijili.service.base.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

@Slf4j
@Api(value = "FansController 关注业务控制器", tags = "关注业务接口模块", produces = "ljj", consumes = "app")
@RestController
@RequestMapping("/api/fans")
public class FansController extends BaseInfoProperties {


    @Resource
    private UsersService usersService;

    @Resource
    private FansService fansService;

    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "我的粉丝列表")
    @GetMapping(value = "/queryMyFans")
    public GraceJSONResult queryMyFans(@RequestParam(value = "myId") String myId,
                                       @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PagedGridResult pagedGridResult = this.fansService.queryMyFans(myId, page, pageSize);
        return GraceJSONResult.ok(pagedGridResult);
    }


    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "我的关注列表")
    @GetMapping(value = "/queryMyFollows")
    public GraceJSONResult queryMyFollows(@RequestParam(value = "myId") String myId,
                                          @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                          @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        PagedGridResult pagedGridResult = this.fansService.queryMyFollows(myId, page, pageSize);
        return GraceJSONResult.ok(pagedGridResult);
    }

    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "是否关注博主")
    @GetMapping(value = "/queryDoIFollowVloger")
    public GraceJSONResult queryDoIFollowVloger(@RequestParam(value = "myId") @NotBlank(message = "粉丝id不能为空")
                                                        String usersId,
                                                @RequestParam(value = "vlogerId") @NotBlank(message = "作者id不能为空")
                                                        String vlogerId) {

        // 传统的方式
        //boolean b = this.fansService.queryDoIFollowVloger(usersId, vlogerId);

        // 查询redis
        String followVloger = this.redis.get(REDIS_FANS_AND_VLOGGER_RELATIONSHIP + ":" + usersId + ":" + vlogerId);
        return GraceJSONResult.ok(!StringUtils.isBlank(followVloger));
    }


    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "取消关注")
    @PostMapping(value = "/cancel")
    public GraceJSONResult cancel(@RequestParam(value = "myId") @NotBlank(message = "粉丝id不能为空")
                                          String usersId,
                                  @RequestParam(value = "vlogerId") @NotBlank(message = "作者id不能为空")
                                          String vlogerId) {
        // 取消关注操作
        this.fansService.doCancel(usersId, vlogerId);

        //TODO  博主粉丝-1, 我的关注-1
        redis.decrement(REDIS_MY_FOLLOWS_COUNTS + ":" + usersId, 1);
        redis.decrement(REDIS_MY_FANS_COUNTS + ":" + vlogerId, 1);
//        redis.decrement(REDIS_MY_FOLLOWS_COUNTS + ":" + usersId, 1);
//        redis.decrement(REDIS_MY_FANS_COUNTS + ":" + vlogerId, 1);

        //TODO 我和博主的关联关系
        //redis_fans_and_vlogger_relationship:123123:444333
        redis.del(REDIS_FANS_AND_VLOGGER_RELATIONSHIP + ":" + usersId + ":" + vlogerId);

        return GraceJSONResult.ok();

    }


    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "我的关注")
    @PostMapping(value = "/follow")
    public GraceJSONResult follow(@RequestParam(value = "myId") @NotBlank(message = "粉丝id不能为空")
                                          String usersId,
                                  @RequestParam(value = "vlogerId") @NotBlank(message = "作者id不能为空")
                                          String vlogerId) {

        //TODO [1] 关注我自己
        if (usersId.equalsIgnoreCase(vlogerId)) {
            return GraceJSONResult.ok("我关注了我自己");
        }

        //TODO [2] 两个用户是否存在, [2.1] 分开查询 ,[2.2] 合并查询

        Users users = this.usersService.getById(usersId);
        Users vlogUers = this.usersService.getById(vlogerId);

        if (users == null && vlogUers == null) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FAN_ERROR_EQ_NULL);
        }
        this.fansService.doFollow(usersId, vlogerId);

        //TODO  博主粉丝+1, 我的关注＋1
        redis.getRedisTemplate().opsForValue().increment(REDIS_MY_FOLLOWS_COUNTS + ":" + usersId, 1);
        redis.getRedisTemplate().opsForValue().increment(REDIS_MY_FANS_COUNTS + ":" + vlogerId, 1);
//        redis.increment(REDIS_MY_FOLLOWS_COUNTS + ":" + usersId, 1);
//        redis.increment(REDIS_MY_FANS_COUNTS + ":" + vlogerId, 1);

        //TODO 我和博主的关联关系
        //redis_fans_and_vlogger_relationship:123123:444333
        redis.set(REDIS_FANS_AND_VLOGGER_RELATIONSHIP + ":" + usersId + ":" + vlogerId, "1");
//        redis.set(REDIS_FANS_AND_VLOGGER_RELATIONSHIP + ":" + usersId + ":" + vlogerId, "1");

        return GraceJSONResult.ok();
    }
}
